package org.jeecg.modules.chvegetable.nyncApi.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.chvegetable.nyncApi.entity.Address;
import org.jeecg.modules.chvegetable.nyncApi.entity.Nync;
import org.jeecg.modules.chvegetable.nyncApi.entity.OrderNumber;
import org.jeecg.modules.chvegetable.nyncApi.entity.Sales;
import org.jeecg.modules.chvegetable.nyncApi.service.INyncService;
import org.jeecg.modules.flower.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 农业农村厅接口
 * @Author: drt
 * @Date: 2023-04-21
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "农业农村厅接口")
@RestController
@RequestMapping("/nynct")
public class NyncController {
    public static final int times = 60;//判断是否可以再次调接口获取新数据的时间间隔（秒）

    @Autowired
    private INyncService nyncService;

    @Autowired
    private RedisUtil redisUtil;

    
    /**
     * 获取入库订单的数量
     *
     * @param
     * @return
     */
    @AutoLog(value = "获取入库订单的数量")
    @ApiOperation(value = "获取入库订单的数量", notes = "获取入库订单的数量")
    @GetMapping(value = "/getInNumber")
    public Result<?> getInNumber() {
        List<OrderNumber> resultList = null;
        Map<String, String> houseNames = CommonUtil.getHouseNames();
        long nowTime = new Date().getTime();//获取当前时间的时间戳
        /*
         * 判断redis中是否有nynctInNumberTime，如果没有nynctInNumberTime的话则获取最新的数据，
         * 然后在redis中存入当前时间的时间戳和获取的列表数据
         * */
        if (redisUtil.get("nynctInNumberTime") == null) {
            resultList = nyncService.getInNumber();
            //将库房的编码转换为库房的名称
            for (OrderNumber orderNumber : resultList) {
                orderNumber.setStorehouse(houseNames.get(orderNumber.getStorehouse()));
            }

            redisUtil.set("nynctInNumberTime", nowTime);
            redisUtil.set("nynctInNumberList", resultList);
        } else {
            //获取redis中已存的时间戳
            long redisTime = Long.parseLong(String.valueOf(redisUtil.get("nynctInNumberTime")));
            /*
             * 获取当前时间的时间戳和redis中已存的时间戳的差值（秒），
             * 如果差值大于配置的数值，那么获取最新的列表数据，然后在redis中存入当前时间的时间戳和获取的列表数据，
             * 如果差值小于配置的数值，那么直接从redis中获取已存入的列表数据
             * */
            int seconds = (int) ((nowTime - redisTime) / 1000);
            if (seconds >= times) {
                resultList = nyncService.getInNumber();
                //将库房的编码转换为库房的名称
                for (OrderNumber orderNumber : resultList) {
                    orderNumber.setStorehouse(houseNames.get(orderNumber.getStorehouse()));
                }

                redisUtil.set("nynctInNumberTime", nowTime);
                redisUtil.set("nynctInNumberList", resultList);
            } else {
                resultList = (List<OrderNumber>) redisUtil.get("nynctInNumberList");
            }
        }
        return Result.OK(resultList);
    }

    /**
     * 获取出库订单的数量
     *
     * @param
     * @return
     */
    @AutoLog(value = "获取出库订单的数量")
    @ApiOperation(value = "获取出库订单的数量", notes = "获取出库订单的数量")
    @GetMapping(value = "/getOutNumber")
    public Result<?> getOutNumber() {
        List<OrderNumber> resultList = null;
        Map<String, String> houseNames = CommonUtil.getHouseNames();
        long nowTime = new Date().getTime();//获取当前时间的时间戳
        /*
         * 判断redis中是否有nynctOutNumberTime，如果没有nynctOutNumberTime的话则获取最新的数据，
         * 然后在redis中存入当前时间的时间戳和获取的列表数据
         * */
        if (redisUtil.get("nynctOutNumberTime") == null) {
            resultList = nyncService.getOutNumber();
            //将库房的编码转换为库房的名称
            for (OrderNumber orderNumber : resultList) {
                orderNumber.setStorehouse(houseNames.get(orderNumber.getStorehouse()));
            }

            redisUtil.set("nynctOutNumberTime", nowTime);
            redisUtil.set("nynctOutNumberList", resultList);
        } else {
            //获取redis中已存的时间戳
            long redisTime = Long.parseLong(String.valueOf(redisUtil.get("nynctOutNumberTime")));
            /*
             * 获取当前时间的时间戳和redis中已存的时间戳的差值（秒），
             * 如果差值大于配置的数值，那么获取最新的列表数据，然后在redis中存入当前时间的时间戳和获取的列表数据，
             * 如果差值小于配置的数值，那么直接从redis中获取已存入的列表数据
             * */
            int seconds = (int) ((nowTime - redisTime) / 1000);
            if (seconds >= times) {
                resultList = nyncService.getOutNumber();
                //将库房的编码转换为库房的名称
                for (OrderNumber orderNumber : resultList) {
                    orderNumber.setStorehouse(houseNames.get(orderNumber.getStorehouse()));
                }

                redisUtil.set("nynctOutNumberTime", nowTime);
                redisUtil.set("nynctOutNumberList", resultList);
            } else {
                resultList = (List<OrderNumber>) redisUtil.get("nynctOutNumberList");
            }
        }
        return Result.OK(resultList);
    }
    
    
    /**
     * 获取订单的销售额
     *
     * @param
     * @return
     */
    @AutoLog(value = "获取订单的销售额")
    @ApiOperation(value = "获取订单的销售额", notes = "获取订单的销售额")
    @GetMapping(value = "/getOrderSales")
    public Result<?> getOrderSales() {
        List<Sales> resultList = null;
        Map<String, String> houseNames = CommonUtil.getHouseNames();
        long nowTime = new Date().getTime();//获取当前时间的时间戳
        /*
         * 判断redis中是否有nynctSalesTime，如果没有nynctSalesTime的话则获取最新的数据，
         * 然后在redis中存入当前时间的时间戳和获取的列表数据
         * */
        if (redisUtil.get("nynctSalesTime") == null) {
            resultList = nyncService.getOrderSales();
            //将库房的编码转换为库房的名称
            for (Sales Sales : resultList) {
                Sales.setStorehouse(houseNames.get(Sales.getStorehouse()));
            }

            redisUtil.set("nynctSalesTime", nowTime);
            redisUtil.set("nynctSalesList", resultList);
        } else {
            //获取redis中已存的时间戳
            long redisTime = Long.parseLong(String.valueOf(redisUtil.get("nynctSalesTime")));
            /*
             * 获取当前时间的时间戳和redis中已存的时间戳的差值（秒），
             * 如果差值大于配置的数值，那么获取最新的列表数据，然后在redis中存入当前时间的时间戳和获取的列表数据，
             * 如果差值小于配置的数值，那么直接从redis中获取已存入的列表数据
             * */
            int seconds = (int) ((nowTime - redisTime) / 1000);
            if (seconds >= times) {
                resultList = nyncService.getOrderSales();
                //将库房的编码转换为库房的名称
                for (Sales Sales : resultList) {
                    Sales.setStorehouse(houseNames.get(Sales.getStorehouse()));
                }

                redisUtil.set("nynctSalesTime", nowTime);
                redisUtil.set("nynctSalesList", resultList);
            } else {
                resultList = (List<Sales>) redisUtil.get("nynctSalesList");
            }
        }
        return Result.OK(resultList);
    }

    /**
     * 获取订单的销售地
     *
     * @param
     * @return
     */
    @AutoLog(value = "获取订单的销售地")
    @ApiOperation(value = "获取订单的销售地", notes = "获取订单的销售地")
    @GetMapping(value = "/getOrderAddress")
    public Result<?> getOrderAddress() {
        List<Address> resultList = null;
        Map<String, String> houseNames = CommonUtil.getHouseNames();
        long nowTime = new Date().getTime();//获取当前时间的时间戳
        /*
         * 判断redis中是否有nynctAddressTime，如果没有nynctAddressTime的话则获取最新的数据，
         * 然后在redis中存入当前时间的时间戳和获取的列表数据
         * */
        if (redisUtil.get("nynctAddressTime") == null) {
            resultList = nyncService.getOrderAddress();
            //将库房的编码转换为库房的名称
            for (Address Address : resultList) {
                Address.setStorehouse(houseNames.get(Address.getStorehouse()));
            }

            redisUtil.set("nynctAddressTime", nowTime);
            redisUtil.set("nynctAddressList", resultList);
        } else {
            //获取redis中已存的时间戳
            long redisTime = Long.parseLong(String.valueOf(redisUtil.get("nynctAddressTime")));
            /*
             * 获取当前时间的时间戳和redis中已存的时间戳的差值（秒），
             * 如果差值大于配置的数值，那么获取最新的列表数据，然后在redis中存入当前时间的时间戳和获取的列表数据，
             * 如果差值小于配置的数值，那么直接从redis中获取已存入的列表数据
             * */
            int seconds = (int) ((nowTime - redisTime) / 1000);
            if (seconds >= times) {
                resultList = nyncService.getOrderAddress();
                //将库房的编码转换为库房的名称
                for (Address Address : resultList) {
                    Address.setStorehouse(houseNames.get(Address.getStorehouse()));
                }

                redisUtil.set("nynctAddressTime", nowTime);
                redisUtil.set("nynctAddressList", resultList);
            } else {
                resultList = (List<Address>) redisUtil.get("nynctAddressList");
            }
        }
        return Result.OK(resultList);
    }

    //将库房的编码转换为库房的名称
    public List<Nync> getHouseNames(List<Nync> list) {
        Map<String, String> houseNames = CommonUtil.getHouseNames();
        for (Nync nync : list) {
            nync.setStorehouse(houseNames.get(nync.getStorehouse()));
        }
        return list;
    }
}
